package com.jm.common.conf.mybatis;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author kong
 */
public class EnhanceBatchDelete extends AbstractMethod {

    public EnhanceBatchDelete() {
        this("enhanceBatchDelete");
    }

    public EnhanceBatchDelete(String methodName) {
        super(methodName);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        //逻辑删除
        final String sql = "<script>update "
                + tableInfo.getTableName()
                + " set deleted=1"
                + " where "
                + tableInfo.getKeyColumn()
                + " in(<foreach collection=\"list\" item=\"i\" separator=\",\">#{i}</foreach>)</script>";

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        final String id = "enhanceBatchDelete";
        return this.addDeleteMappedStatement(mapperClass, id, sqlSource);
    }

}
